import PerfectLib
import PerfectHTTP
import StORM
import MySQLStORM
import PerfectSession
import PerfectLogger
let ECAppProjectURL = "/ECApp/api/v1"

let sessionManager = MySQLSessionsManage()

public class ECAppConfig {
   
    private static var executeInit = false
   
    public static func `init`(){
        guard !executeInit else {
            return
        }
        LogFile.info("ECAppConfig初始化")
        initDatabase()
        initTablesInDatabase()
        executeInit = true
       
    }
    
    public static func initDatabase(){
         LogFile.info("配置数据库连接参数")
        MySQLConnector.host        = "127.0.0.1"
        MySQLConnector.username    = "root"
        MySQLConnector.password    = "test123"
        MySQLConnector.database    = "perfect_testing"
        MySQLConnector.port        = 3306
       
    }
    public static func initTablesInDatabase(){
         LogFile.info("创建数据库表")
        let sessionORM = PerfectSessionORM()
        try? sessionORM.setup()
        
    }
    
    
    public  static func getRoutes()-> Routes {
        var routes = Routes(baseUri: ECAppProjectURL)
       
        routes.add(ecAppRoutes)
        return routes
     
    }
    
    public static func getRequestFilters() -> [(HTTPRequestFilter, HTTPFilterPriority)]{
        // 配置权限
        // 不需要的认证的地址
        let exludePath = Path.authFreePaths
        AuthFilter.authenticationConfig.exclude(exludePath.map({ ECAppProjectURL + $0.rawValue}))
        
        // 需要认证的地址
         let inculdePath = Path.authPaths
        AuthFilter.authenticationConfig.include(inculdePath.map({ ECAppProjectURL + $0}))
  
        
       return [
             (AuthFilter.filterRoutes(),HTTPFilterPriority.high),
              (FilterMethod(),HTTPFilterPriority.high)
             ]
       
    }
    
//    public static func getResponseFilters() -> [(HTTPResponseFilter, HTTPFilterPriority)]{
//         return [(Filter404(),HTTPFilterPriority.high)]
//    }
}


